<template>
    <el-container>
        <el-main class="wrapper">
            <v-breadcrumb v-bind:breadcumb="breadcumb"></v-breadcrumb>
            <section class="panel" :style="{'min-height': mainHeight+'px'}">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                    </div>
                    <el-table  v-loading="loading"
                               element-loading-text="努力加载中..."
                               element-loading-spinner="el-icon-loading"
                               :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="id" label="编号" width="80px"></el-table-column>
                        <el-table-column prop="openid" label="openid" width="350px"></el-table-column>
                        <el-table-column prop="reg_at" label="注册时间" width="150px">
                            <template slot-scope="scope">{{ scope.row.reg_at ? moment(scope.row.reg_at).format('YYYY-MM-DD HH:mm') : ''}}</template>
                        </el-table-column>
                        <el-table-column prop="login_at" label="最近登录" width="150px">
                            <template slot-scope="scope">{{ scope.row.login_at ? moment(scope.row.login_at).format('YYYY-MM-DD HH:mm') : ''}}</template>
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button icon="fa fa-trash"
                                           @click="destory(scope.row)" type="danger" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <div class="pagination-total">共 <em>{{pageData.total}}</em> 个用户</div>
                        <el-pagination  v-if="!loading && pageData && pageData.last_page > 1" background layout="prev, pager, next, jumper"
                                        :page-size="pageData.per_page"
                                        :total="pageData.total" :current-page="pageData.current_page"
                                        @current-change="handleCurrentChange">
                        </el-pagination>
                    </div>
                </div>
            </section>
        </el-main>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import vBreadcrumb from '../layout/breadcrumb.vue';
    import {UserList} from '../../utils/axios';
    export default {
        components:{vBreadcrumb},
        data(){
            return {
                breadcumb:[{name: "用户管理"},],
                tableData:[],//列表数据
                pageData:[],//分页数据
                search: {page:null},//筛选
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/user');
            this.list();
        },
        computed: {
            ...mapGetters(['active','mainHeight','loading'])
        },
        methods:{
            //分页
            handleCurrentChange(val) {
                this.search.page = val;
                this.list();
            },
            //获取列表
            list(){
                this.$store.dispatch('setLoading', true);
                UserList({params: this.search}).then((response)=>{
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data.data;
                        this.pageData = response.data;
                    }
                })
            },
        },
    }
</script>
